package cn.xinliao.activity;

import org.json.JSONObject;

import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import cn.xinliao.R;
import cn.xinliao.utility.PublicData;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LoginActivity extends Activity implements OnClickListener {

	private static final String APP_ID = "100451352";
	private static final String SCOPE = "all";
	private Tencent mTencent;
	private ImageView mLoginButton;
	private TextView mBaseMessageText;
	private TextView mMessageText;
	private Handler mHandler;
	private Dialog mProgressDialog;
	private EditText ed_userName;
	private EditText ed_psd;
	private CheckBox cb_savepsd;
	private Button bt_login;
	private Button bt_register;
	private TextView renren_forgetpsd;
	private ImageView iv_qqlogin;
	private LinearLayout forgetLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		// 隐藏软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		ed_userName = (EditText) this.findViewById(R.id.login_userName);
		ed_psd = (EditText) this.findViewById(R.id.login_Password);
		cb_savepsd = (CheckBox) findViewById(R.id.savePasswordCB);
		bt_login = (Button) this.findViewById(R.id.login_btn);
		bt_register = (Button) findViewById(R.id.register_btn);
		renren_forgetpsd = (TextView) findViewById(R.id.renren_login_btn);
		iv_qqlogin = (ImageView) findViewById(R.id.qq_login_btn);

		bt_login.setOnClickListener(this);

		cb_savepsd
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						onSaveData(isChecked);
					}

					{
						if (PublicData.bRemberInfo) {
							ed_userName.setText(PublicData.userName);
							ed_psd.setText(PublicData.passWord);
							cb_savepsd.setChecked(true);
						}
					}

					void onSaveData(boolean bSave) {
						if (bSave) {
							// 保存数据到本地
							PublicData.userName = (String) ed_userName
									.getText().toString();
							PublicData.passWord = (String) ed_psd.getText()
									.toString();
							PublicData.bRemberInfo = true;
						} else {
							// 不保存，清掉本地数据
							PublicData.userName = "";
							PublicData.passWord = "";
							PublicData.bLogined = false;
							PublicData.bRemberInfo = false;
						}
						PublicData.saveData(getApplicationContext());
					}
				});

		// 响应 注册按钮
		bt_register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(LoginActivity.this, RegisterActivity.class);
				startActivity(intent);
			}
		});


		/*//
		iv_qqlogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			//	Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
					onClickLogin();
				//	v.startAnimation(shake);
			}
		});*/
		
		// Tencent类是SDK的主要实现类，开发者可通过Tencent类访问腾讯开放的OpenAPI。
		// 其中APP_ID是分配给第三方应用的appid，类型为String。
		mTencent = Tencent.createInstance(APP_ID, this.getApplicationContext());
		// 1.4版本:此处需新增参数，传入应用程序的全局context，可通过activity的getApplicationContext方法获取
		// 初始化视图
		initViews();
	}

	private void initViews() {
		mLoginButton = (ImageView) findViewById(R.id.qq_login_btn);
		updateLoginButton();

		mLoginButton.setOnClickListener(this);
	}

	private void updateLoginButton() {
		mLoginButton
				.setImageResource(mTencent.isSessionValid() ? R.drawable.com_tencent_open_logout
						: R.drawable.com_tencent_open_login);
	}

	@Override
	public void onClick(View v) {
		Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
		switch (v.getId()) {
		case R.id.qq_login_btn:
			onClickLogin();
			v.startAnimation(shake);
			break;

		default:
			break;
		}
	}

	private void onClickLogin() {
		if (!mTencent.isSessionValid()) {
			IUiListener listener = new BaseUiListener() {
				@Override
				protected void doComplete(JSONObject values) {
					updateLoginButton();
				}
			};
			mTencent.login(this, SCOPE, listener);
		} else {
			mTencent.logout(this);
			updateLoginButton();
		}
	}

	private class BaseUiListener implements IUiListener {
		@Override
		public void onComplete(JSONObject response) {
			mBaseMessageText.setText("onComplete:");
			mMessageText.setText(response.toString());
			doComplete(response);
		}

		protected void doComplete(JSONObject values) {

		}

		@Override
		public void onError(UiError e) {
			showResult("onError:", "code:" + e.errorCode + ", msg:"
					+ e.errorMessage + ", detail:" + e.errorDetail);
		}

		@Override
		public void onCancel() {
			showResult("onCancel", "");
		}
	}

	private void showResult(final String base, final String msg) {
		mHandler.post(new Runnable() {

			@Override
			public void run() {
				if (mProgressDialog.isShowing())
					mProgressDialog.dismiss();
				mBaseMessageText.setText(base);
				mMessageText.setText(msg);
			}
		});
	}


}
